﻿--HEADER Add trigger Trigger_TradeToIdea_Timestamp; Rename LinkerEntityCode column on CreatorCode; Add CreatorCode column to Idea, SecurityToIdea tables

CREATE TRIGGER [UDS].[Trigger_TradeToIdea_Timestamp]
    ON [UDS].[TradeToIdea]
    FOR INSERT, UPDATE
    AS
    BEGIN
        UPDATE [UDS].[TradeToIdea]
		SET [Timestamp] = GETDATE()
		FROM Inserted i
		WHERE UDS.TradeToIdea.IdeaId = i.IdeaId AND UDS.TradeToIdea.TradeId = i.TradeId
    END
GO

ALTER TABLE [UDS].[TradeToIdea]
ALTER COLUMN LinkerEntityCode TINYINT

exec sp_rename 'UDS.TradeToIdea.LinkerEntityCode' , 'CreatorCode', 'COLUMN'


ALTER TABLE [UDS].[Idea] ADD CreatorCode TINYINT NOT NULL
CONSTRAINT [DF_Idea_CreatorCode] DEFAULT 0

ALTER TABLE [UDS].[Idea] DROP CONSTRAINT [DF_Idea_CreatorCode]


ALTER TABLE [UDS].[SecurityToIdea] ADD CreatorCode TINYINT NOT NULL
CONSTRAINT [DF_SecurityToIdea_CreatorCode] DEFAULT 0

ALTER TABLE [UDS].[SecurityToIdea] DROP CONSTRAINT [DF_SecurityToIdea_CreatorCode]

--NULL - Directional Type
UPDATE [UDS].[Idea]
SET [Type] = 2
WHERE [Type] IS NULL

ALTER TABLE [UDS].[Idea]
ALTER COLUMN [Type] TINYINT NOT NULL